package com.example.service;

import com.example.raft.RaftServer;
import com.example.utils.IPUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

/**
 * @className: RaftServerListener
 * @author: chentao
 * @date: 2025/9/18 8:33
 * @Version: 1.0
 * @description:
 */
@Service
public class RaftServerListener implements InitializingBean, DisposableBean {

    Logger logger = LoggerFactory.getLogger(RaftServerListener.class);

    public static final String DEFAULT_SERVER = "server_";

    private RaftServer raftServer;

    @Override
    public void afterPropertiesSet() throws Exception {
        this.raftServer = new RaftServer(DEFAULT_SERVER + IPUtils.getServer());
        this.raftServer.start();
    }

    @Override
    public void destroy() throws Exception {
        this.raftServer.stop();
    }

    public RaftServer getRaftServer() {
        return raftServer;
    }
}
